package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameMIDlet;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.game.ActorItem;
import com.handinfo.game.ActorRole;
import com.handinfo.game.GameScene;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

public class UIDeal extends UIAbstractWindow implements ILBHandler
{
	// STATE
	private final int STATE_DEAL_BASE = 1;
	// GROUP
	private final int GROUP_DEAL_SELF = 1;
	private final int GROUP_DEAL_OTHER = 2;
	private final int GROUP_MONEY_DEAL = 3;
	private final int GROUP_LABEL = 4;
	private final int GROUP_BAG = 5;
	// NUM
	private final int NUM_ROWS_DEAL = 2;
	private final int NUM_COLS_DEAL = 3;
	// Index
	private final int INDEX_DEAL_SELF_FIRST = 1;
	private final int INDEX_DEAL_OTHER_FIRST = INDEX_DEAL_SELF_FIRST + NUM_COLS_DEAL;
	private final int INDEX_DEAL_SELF_LAST = INDEX_DEAL_OTHER_FIRST + (NUM_ROWS_DEAL * NUM_COLS_DEAL) - 1;
	private final int INDEX_DEAL_OTHER_LAST = INDEX_DEAL_SELF_LAST + NUM_COLS_DEAL;
	private final int INDEX_DEAL_MONEY = INDEX_DEAL_OTHER_LAST + 1;
	private final int INDEX_DEAL_LABEL_FIRST = INDEX_DEAL_MONEY + 1;
	private final int INDEX_DEAL_LABEL_LAST = INDEX_DEAL_LABEL_FIRST + ActorItemLBHandler.NUM_LABEL_DIS - 1;
	private final int INDEX_DEAL_BAG_FIRST = INDEX_DEAL_LABEL_LAST + 1;
	// WH
	private final int IW_DEALITEM = 3;// interval W
	private final int IH_DEALITEM = 3;
	private final int RW_EQUIP = UITools.W_BAG + IW_DEALITEM;// interval W
	private final int RH_EQUIP = UITools.H_BAG + IH_DEALITEM;
	//
	private final int W_DEALBAR = ((UITools.W_RECT - UITools.IW_RECTS * 3) >> 1);
	private final int H_DEALBAR = 90;
	// var
	private String m_name_other = "";
	private int m_level_self;
	private int m_level_other;
	private ActorItem[] m_items_self;
	private ActorItem[] m_items_other;
	private ActorItem[] m_items_bag_temp;
	private long m_money_game_other;
	private long m_money_game_self;
	private long m_money_game_now = GameManager.getInstance().m_role.m_money_game;
	//
	private ActorItemLBHandler m_bag_handler;
	private UIMessageBox m_messageBox_leftsoft;// 点击左软键开启的确认框
	private UIMessageBox m_messageBox_rightsoft;// 点击y右软键开启的确认框
	private static UIMessageBox m_isDealWidthA;// 是否和A交易选择框

	public UIDeal(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		//
		initDate();
	}

	private void initDate()
	{
		m_items_self = new ActorItem[NUM_COLS_DEAL * NUM_ROWS_DEAL];
		m_items_other = new ActorItem[NUM_COLS_DEAL * NUM_ROWS_DEAL];
		m_items_bag_temp = new ActorItem[ActorRole.NUM_BAG_GOODS];
		//
		ActorItem[] items = GameManager.getInstance().m_role.m_bag_goods;
		for (int i = 0; i < m_items_bag_temp.length; i++) {
			m_items_bag_temp[i] = items[i];
		}
		initSoftMessage();
	}

	public void createNotify()
	{
		m_bag_handler = new ActorItemLBHandler(this);
		m_bag_handler.setLabelByGroupIndex(0);
		// 初始化状态及索引
		this.setWindowStatus(STATE_DEAL_BASE);
		this.setWindowIndex(INDEX_DEAL_BAG_FIRST);
	}

	public int countGroupId(int mIndex)
	{
		if (mIndex < INDEX_DEAL_OTHER_FIRST) {//
			return GROUP_DEAL_SELF;
		}
		else if (mIndex < (INDEX_DEAL_SELF_FIRST + (NUM_COLS_DEAL << 1))) {
			return GROUP_DEAL_OTHER;
		}
		else if (mIndex < (INDEX_DEAL_OTHER_FIRST + (NUM_COLS_DEAL << 1))) {
			return GROUP_DEAL_SELF;
		}
		else if (mIndex < INDEX_DEAL_MONEY) {
			return GROUP_DEAL_OTHER;
		}
		else if (mIndex < INDEX_DEAL_LABEL_FIRST) {
			return GROUP_MONEY_DEAL;
		}
		else if (mIndex < INDEX_DEAL_BAG_FIRST) {
			return GROUP_LABEL;
		}
		else {
			return GROUP_BAG;
		}
	}

	public int countGroupIndex(int mGroupId, int mIndex)
	{
		switch (mGroupId) {
			case GROUP_DEAL_SELF:
				if (this.m_index < INDEX_DEAL_OTHER_FIRST) {// 第一行
					return this.m_index - INDEX_DEAL_SELF_FIRST;
				}
				else {
					return this.m_index - INDEX_DEAL_SELF_FIRST - NUM_COLS_DEAL;
				}
			case GROUP_DEAL_OTHER:
				if (this.m_index > INDEX_DEAL_SELF_LAST) {// 第二行
					return this.m_index - INDEX_DEAL_OTHER_FIRST - NUM_COLS_DEAL;
				}
				else {
					return this.m_index - INDEX_DEAL_OTHER_FIRST;
				}
			case GROUP_MONEY_DEAL:
				return 0;
			case GROUP_LABEL:
				return this.m_index - INDEX_DEAL_LABEL_FIRST;
			case GROUP_BAG:
				return this.m_index - INDEX_DEAL_BAG_FIRST;
			default:
				Tools.debugPrintln("无此GroupId = " + mGroupId);
				return 1;
		}
	}

	public void actionOnIndexChanged()
	{
		m_bag_handler.updateOnStateOrIndexChanged();
	}

	/***************************/
	// IHandler
	/***************************/

	public Object[] getBagDatas()
	{
		return m_items_bag_temp;
	}

	public int getUpIndex()
	{
		switch (this.m_groupId) {
			case GROUP_LABEL:
				return INDEX_DEAL_MONEY;
			case GROUP_BAG:
				return INDEX_DEAL_LABEL_FIRST;
			default:
				Tools.debugPrintln("无此GroupId = " + this.m_groupId);
				return 1;
		}
	}

	public int getDownIndex()
	{
		switch (this.m_groupId) {
			case GROUP_LABEL:
				return INDEX_DEAL_BAG_FIRST;
			case GROUP_BAG:
				return INDEX_DEAL_SELF_FIRST;
			default:
				Tools.debugPrintln("无此GroupId = " + this.m_groupId);
				return 1;
		}
	}

	public int getLeftIndex()
	{
		switch (this.m_groupId) {
			case GROUP_LABEL:
				return INDEX_DEAL_MONEY;
			case GROUP_BAG:
				return INDEX_DEAL_LABEL_LAST;
			default:
				Tools.debugPrintln("无此GroupId = " + this.m_groupId);
				return 1;
		}
	}

	public int getRightIndex()
	{
		switch (this.m_groupId) {
			case GROUP_LABEL:
				return INDEX_DEAL_BAG_FIRST;
			case GROUP_BAG:
				return INDEX_DEAL_SELF_FIRST;
			default:
				Tools.debugPrintln("无此GroupId = " + this.m_groupId);
				return 1;
		}
	}

	public int getFirstIndex(int idComp)
	{
		switch (idComp) {
			case AbsLBHandler.TYPE_LABEL:
				return INDEX_DEAL_LABEL_FIRST;
			case AbsLBHandler.TYPE_BAG:
				return INDEX_DEAL_BAG_FIRST;
			default:
				Tools.debugPrintln("无此GroupId = " + idComp);
				return 1;
		}
	}

	public int getGroupIndex()
	{
		return this.m_groupIndex;
	}

	public int getWindowIndex()
	{
		return this.m_index;
	}

	/***************************/
	// 按键处理
	/***************************/

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			UIManager.getInstance().addWindow(m_messageBox_leftsoft);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			UIManager.getInstance().addWindow(m_messageBox_rightsoft);
		}
		else {
			switch (this.m_groupId) {
				case GROUP_DEAL_SELF:
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
						if (this.m_groupIndex >= ((NUM_ROWS_DEAL - 1) * NUM_COLS_DEAL)) {//第二行
							this.setWindowIndex(this.m_index - (NUM_COLS_DEAL << 1));
						}
						else {
							this.setWindowIndex(INDEX_DEAL_MONEY);
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
						if (this.m_groupIndex < NUM_COLS_DEAL) {//第一行
							this.setWindowIndex(this.m_index + (NUM_COLS_DEAL << 1));
						}
						else {
							this.setWindowIndex(INDEX_DEAL_MONEY);
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
						if (this.m_groupIndex == 0 || this.m_groupIndex == NUM_COLS_DEAL) {//左边第一个
							this.setWindowIndex(m_index + (NUM_COLS_DEAL << 1) - 1);
						}
						else {
							this.setWindowIndex(this.m_index - 1);
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
						this.setWindowIndex(this.m_index + 1);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						clickItemEquipSelf();
					}
					break;
				case GROUP_DEAL_OTHER:
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
						if (this.m_groupIndex >= 3) {//第二行
							this.setWindowIndex(this.m_index - (NUM_COLS_DEAL << 1));
						}
						else {
							this.setWindowIndex(this.m_index + (NUM_ROWS_DEAL - 1) * (NUM_COLS_DEAL << 1));
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
						if (this.m_groupIndex >= 3) {//第二行
							this.setWindowIndex(this.m_index - (NUM_ROWS_DEAL - 1) * (NUM_COLS_DEAL << 1));
						}
						else {
							this.setWindowIndex(this.m_index + (NUM_COLS_DEAL << 1));
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
						this.setWindowIndex(this.m_index - 1);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
						int lineNum = (NUM_COLS_DEAL << 1);
						if (this.m_groupIndex == 2 || m_groupIndex == 5) {
							this.setWindowIndex(m_index - (lineNum - 1));
						}
						else {
							this.setWindowIndex(this.m_index + 1);
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						clickItemEquipOther();
					}
					break;
				case GROUP_MONEY_DEAL:
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
						this.setWindowIndex(INDEX_DEAL_SELF_FIRST + (NUM_COLS_DEAL << 1));
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
						this.setWindowIndex(m_bag_handler.getCurrentLabelWindowIndex());
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
//						this.setWindowIndex(this.m_index - 1);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
//						this.setWindowIndex(this.m_index + 1);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						clickDealMoney();
					}
					break;
				case GROUP_LABEL: {
					int dir = m_bag_handler.keyDir(AbsLBHandler.TYPE_LABEL);
					if (dir != AbsLBHandler.TYPE_DIR_NONE) {
						this.setWindowIndex(dir);
					}
				}
					break;
				case GROUP_BAG: {
					int dir = m_bag_handler.keyDir(AbsLBHandler.TYPE_BAG);
					if (dir == AbsLBHandler.TYPE_DIR_NONE) {
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
								|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
							clickItemBag();
						}
					}
					else {
						this.setWindowIndex(dir);
					}
				}
					break;
			}
		}
	}

	private void initSoftMessage()
	{
		m_messageBox_leftsoft = (UIMessageBox) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		m_messageBox_leftsoft.setMessage("是否确定交易");
		m_messageBox_leftsoft.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					GameManager.getInstance().m_game_message.sendDealConfig();
					UIManager.getInstance().removeWindow(m_messageBox_leftsoft);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(m_messageBox_leftsoft);
				}
			}
		});

		m_messageBox_rightsoft = (UIMessageBox) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		m_messageBox_rightsoft.setMessage("是否取消交易");
		m_messageBox_rightsoft.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					GameManager.getInstance().m_game_message.sendDealCancel();
					UIManager.getInstance().removeWindow(m_messageBox_rightsoft);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(m_messageBox_rightsoft);
				}
			}
		});
	}

	/**
	 * 点击自己的交易格子
	 */
	private void clickItemEquipSelf()
	{
		ActorItem item = m_items_self[this.m_groupIndex];
		if (item == null) {
			return;
		}
		if (item.m_isOperLocked) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "道具已经绑定，无法与玩家交易");
			return ;
		}
		if (item.isCanDealWithPlayer()) {
			GameManager.getInstance().m_game_message.sendDealLaydownGoods(this.m_groupIndex);
		}else{
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, ActorItem.getOperTypeErrStr(ActorItem.INDEX_TYPE_OPER_DEAL_WITH_PLAYER)) ;
		}
	}

	/**
	 * 点击他人的交易格子
	 */
	private void clickItemEquipOther()
	{
		// 查看详情
		final ActorItem item = m_items_other[this.m_groupIndex];
		if (item == null) {
			return;
		}
		String[] menus = new String[] { "查看", "返回" };
		final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_POPMENU);
		popmenu.setPopmenus(menus);
		popmenu.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					UIManager.getInstance().removeWindow(popmenu);
					int index = popmenu.getCurrentIndex();
					switch (index) {
						case 0:
							// 查看
							GameManager.getInstance().m_game_message.sendActorItemDetail(item.m_guid);
							break;
						case 1:
						default:
							break;
					}
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(popmenu);
				}
			}
		});
	}

	/**
	 * 点击交易金钱栏
	 */
	private void clickDealMoney()
	{
		Form myForm = new Form("输入交易金额");
		final short[] money = ActorRole.getMoney(m_money_game_self);
		final TextField textField1 = new TextField("金：", String.valueOf(money[0]), 4, TextField.NUMERIC);
		final TextField textField2 = new TextField("银：", String.valueOf(money[1]), 2, TextField.NUMERIC);
		final TextField textField3 = new TextField("铜：", String.valueOf(money[2]), 2, TextField.NUMERIC);
		myForm.append(textField1);
		myForm.append(textField2);
		myForm.append(textField3);
		short[] moneyGame = ActorRole.getMoney(GameManager.getInstance().m_role.m_money_game);
		myForm.append("当前拥有金 : " + moneyGame[0] + " ,银 ：" + moneyGame[1] + " ,铜 ：" + moneyGame[2]);
		final StringItem info = new StringItem("", "");
		myForm.append(info);

		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		//
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d)
			{
				if (c == ok) {
					int gold = UITools.checkMoney(textField1.getString());
					switch (gold) {
						case -1:
						case -2:
							info.setText("请输入正确数据金钱");
							textField1.setString(String.valueOf(money[0]));
							return;
					}
					//
					int silver = UITools.checkMoney(textField2.getString());
					switch (silver) {
						case -1:
						case -2:
							info.setText("请输入正确数据金钱");
							textField2.setString(String.valueOf(money[1]));
							return;
					}
					//
					int copper = UITools.checkMoney(textField3.getString());
					switch (copper) {
						case -1:
						case -2:
							info.setText("请输入正确数据金钱");
							textField3.setString(String.valueOf(money[2]));
							return;
					}
					short[] temp = new short[3];
					temp[0] = (short) gold;
					temp[1] = (short) silver;
					temp[2] = (short) copper;

					long tempMoney = ActorRole.getMoney(temp);

					if (tempMoney > GameManager.getInstance().m_role.m_money_game) {
						info.setText("当前金额不足");
					}
					else {
						Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
						m_money_game_self = tempMoney;
						m_money_game_now = GameManager.getInstance().m_role.m_money_game - tempMoney;
						GameManager.getInstance().m_game_message.sendDealLayupMoney(tempMoney);
					}
				}
				else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}
		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	/**
	 * 点击背包格子
	 */
	private void clickItemBag()
	{
		Tools.debugPrintln("clickItemBag");
		ActorItem item = (ActorItem) m_bag_handler.getCurrentBagItem();
		if (item == null) {
			Tools.debugPrintln("clickItemBag : item is Null");
			return;
		}
		GameManager.getInstance().m_game_message.sendDealLayupGoods(item.m_index);
	}

	/***************************/
	// 绘制
	/***************************/
	private final String TITLE = "交易";
	private final String LEVEL = "级";

	private int m_x_describe;// 描述的pos
	private int m_y_describe;// 描述的pos

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE);
		int tempY = m_y + UITools.INITY_CONTENT;
		paintDealBar(g, m_x + UITools.IW_RECTS, tempY, W_DEALBAR, H_DEALBAR, GameManager.getInstance().m_role.m_name,
				GameManager.getInstance().m_role.m_lv,
				GROUP_DEAL_SELF, m_items_self, m_money_game_self);
		paintDealBar(g, m_x + m_w - UITools.IW_RECTS - W_DEALBAR, tempY, W_DEALBAR, H_DEALBAR, m_name_other,
				m_level_other, GROUP_DEAL_OTHER, m_items_other, m_money_game_other);
		tempY += (UITools.IH_RECT_CONTENT + H_DEALBAR);
		m_bag_handler.setInitXY(tempY);
		m_bag_handler.paintLabel(g);
		m_bag_handler.paintBag(g);
		m_bag_handler.paintDrag(g);
		m_bag_handler.paintBagFocused(g);
		tempY += m_bag_handler.getLBHeight() + UITools.IH_RECTS;
		UITools.paintMoneyGameRB(g, m_x + m_w - UITools.IW_WIN, m_y + UITools.ENDY_CONTENT, m_money_game_now, false);
		paintDescribe(g);
	}

	private void paintDealBar(Graphics g, int x, int y, int w, int h, String name, int level, int groundId,
			ActorItem[] dealItems, long dealMoney)
	{
		UITools.paintContentRect(g, x, y, w, h);
		int tempY = y + UITools.IH_RECT_CONTENT;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
		UITools.paintScrollStr(g, x + UITools.IW_RECT_CONTENT, tempY, w - 30, AbstractGameScreen.FONT_H, name);
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		int tempX = x + w - 2;
		Tools.drawNumber(g, GameScene.s_img_num, tempX - 20, tempY + 6, 5, 7, level, 0, Graphics.TOP | Graphics.RIGHT,
				false);
		g.drawString(LEVEL, tempX, tempY, Graphics.TOP | Graphics.RIGHT);
		//
		tempY += 22;
		paintDealBar(g, x + 10, tempY, groundId, dealItems);
		//
		tempY += 48;
		UITools.paintMoneyGameRB(g, x + w - UITools.IW_RECT_CONTENT, y + h - UITools.IH_RECT_CONTENT, dealMoney,
				groundId == GROUP_DEAL_SELF && this.m_groupId == GROUP_MONEY_DEAL);
	}

	private void paintDescribe(Graphics g)
	{
		switch (this.m_groupId) {
			case GROUP_DEAL_SELF:
				if (m_items_self == null) {
					return;
				}
				UITools.paintItemDescribe(g, m_x_describe, m_y_describe, m_w, m_h, m_items_self[this.m_groupIndex]);
				break;
			case GROUP_DEAL_OTHER:
				if (m_items_other == null) {
					return;
				}
				UITools.paintItemDescribe(g, m_x_describe, m_y_describe, m_w, m_h, m_items_other[this.m_groupIndex]);
				break;
			case GROUP_BAG: {
				m_bag_handler.paintDescribe(g);
			}
				break;
			default:
				break;
		}
	}

	private void paintDealBar(Graphics g, int x, int y, int groupId, ActorItem[] dealItems)
	{
		int num = 0;
		int tempY = y;
		boolean isFocusAble = this.m_groupId == groupId;
		for (int i = 0; i < NUM_ROWS_DEAL; i++) {
			int tempX = x;
			for (int j = 0; j < NUM_COLS_DEAL; j++) {
				boolean isFocused = isFocusAble && num == this.m_groupIndex;
				if (isFocused) {
					m_x_describe = tempX;
					m_y_describe = tempY;
				}
				UITools.paintActorItem(g, tempX, tempY, dealItems[num], false, isFocused, true);
				tempX += RW_EQUIP;
				num++;
			}
			tempY += RH_EQUIP;
		}
	}

	/***************************/
	// 数据接收
	/***************************/

	public static void recvMessage(int orderId, DataInputStream dis)
	{
		UIDeal deal = (UIDeal) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_DEAL);
		Tools.debugPrintln("接收到交易数据");
		if (deal != null) {
			try {
				deal.recvMessages(orderId, dis);
			}
			catch (IOException e) {
			}
		}
	}

	public void recvMessages(int orderId, DataInputStream dis) throws IOException
	{
		switch (orderId) {
			case GameProtocol.CS_DEAL_GOODS_LAYUP_SELF://
				recvLayupGoodsSTA(dis);
				break;
			case GameProtocol.S_DEAL_GOODS_LAYUP_OTHER:
				recvLayupGoodsSTB(dis);
				break;
			case GameProtocol.CS_DEAL_GOODS_LAYDOWN_SELF://
				recvLaydownGoodsSTA(dis);
				break;
			case GameProtocol.S_DEAL_GOODS_LAYDOWN_OTHER://
				recvLaydownGoodsSTB(dis);
				break;
			case GameProtocol.CS_DEAL_MONEY_SELF://
				recvLayMoneySTA(dis);
				break;
			case GameProtocol.S_DEAL_MONEY_OTHER://
				recvLayMoneySTB(dis);
				break;
			case GameProtocol.CS_DEAL_CONFIG:
				recvConfigSTA(dis);
				break;
			case GameProtocol.S_DEAL_CONFIG:
				recvConfigSTB(dis);
				break;
			case GameProtocol.CS_DEAL_CANCEL:
				recvCancelSTA(dis);
				break;
			case GameProtocol.S_DEAL_CANCEL:
				recvCancelSTB(dis);
				break;
		}
	}

	// A向server发送与B建立交易的请求，server向A发送此数据
	public static final void recvRequestSTA(DataInputStream dis)
			throws IOException
	{
		short state = dis.readShort();
		String infos = dis.readUTF();
		if (state == 0) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, infos);
		}
		Tools.debugPrintln("recvRequestSTA :" + "A向server发送与B建立交易的请求，server向A发送此数据 ");
		Tools.debugPrintln("recvRequestSTA :" + " STATE = " + state + " INFO = " + infos);
	}

	// A向server发送与B建立交易的请求，server向B发送此数据
	public static final void recvRequestSTB(DataInputStream dis)
			throws IOException
	{
		String infos = dis.readUTF();
		Tools.debugPrintln("recvRequestSTB :" + "A向server发送与B建立交易的请求，server向B发送此数据 ");
		Tools.debugPrintln("recvRequestSTB :" + " INFO = " + infos);
		m_isDealWidthA = (UIMessageBox) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		m_isDealWidthA.setMessage(infos + "请求与您交易，是否同意");
		m_isDealWidthA.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT) || AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					UIManager.getInstance().removeWindow(m_isDealWidthA);
					GameManager.getInstance().m_game_message.sendDealResponse((byte) 1);// 同意建立连接
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(m_isDealWidthA);
					GameManager.getInstance().m_game_message.sendDealResponse((byte) 0);// 不同意建立连接
				}
			}
		});
	}

	// A向server发送与B建立交易的请求，server向B发送请求，此处回的是B的响应或者超时响应
	public static final void recvResponseSTA(DataInputStream dis)
			throws IOException
	{
		short state = dis.readShort();
		String infos = dis.readUTF();
		Tools.debugPrintln("recvResponseSTA :" + "A向server发送与B建立交易的请求，server向A发送此数据 ");
		Tools.debugPrintln("recvResponseSTA :" + " STATE = " + state + " INFO = " + infos);
		if (state == 0) {
			if (!infos.equals("")) {
			}
			return;
		}
		byte respneseId = dis.readByte();
		Tools.debugPrintln("recvResponseSTA :" + " RESPONSEID = " + respneseId);
		if (respneseId == 0) {// 取消了交易
			return;
		}
		UIDeal deal = (UIDeal) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_DEAL);
		deal.m_name_other = dis.readUTF();
		deal.m_level_other = dis.readInt();
		GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "开始与" + deal.m_name_other + "交易");
	}

	// A向server发送与B建立交易的请求，server向B发送请求，此处回的是B的响应或者超时响应
	public static final void recvResponseSTB(DataInputStream dis)
			throws IOException
	{
		byte responese = dis.readByte();
		Tools.debugPrintln("recvResponseSTB :" + " RESPONSEID = " + responese);
		if (responese == 0) {// B取消了交易，或者时间到了B无响应
			// 此处应该加上消息反馈，协议中无
			UIManager.getInstance().removeWindow(m_isDealWidthA);
			m_isDealWidthA = null;
			// UiManager.getInstance().removeWindowFromWindowList(m_win_isDealWithOther);
			GameManager.getInstance().addMessage("玩家不同意与您交易或者时间过长");
			return;
		}
		UIDeal deal = (UIDeal) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_DEAL);
		deal.m_name_other = dis.readUTF();
		deal.m_level_other = dis.readInt();
		GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "开始与" + deal.m_name_other + "交易");
	}

	/**
	 * A将物品放置到交易栏
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvLayupGoodsSTA(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvLayupGoodsSTA ");
		short state = dis.readShort();
		String infos = dis.readUTF();
		Tools.debugPrintln("recvLayupGoodsSTA :" + "A将物品放置到交易栏,server对A响应 ");
		Tools.debugPrintln("recvLayupGoodsSTA :" + " STATE = " + state + " INFO = " + infos);
		if (state == 0) {
			if (!infos.equals("")) {
				GameManager.getInstance().addMessage(infos);
			}
			return;
		}
		int index_bag = dis.readInt();
		int index_deal = dis.readInt();
		Tools.debugPrintln("recvLayupGoodsSTA data_index_bag = " + index_bag);
		Tools.debugPrintln("recvLayupGoodsSTA data_index_deal = " + index_deal);
		// 设置数据
		m_items_self[index_deal] = m_items_bag_temp[index_bag];
		m_items_bag_temp[index_bag] = null;

		m_bag_handler.resetBagData();

	}

	/**
	 * A 放上道具，Server对B的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvLayupGoodsSTB(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvLayupGoodsSTB");
		int index_deal = dis.readInt();
		Tools.debugPrintln("recvLayupGoodsSTB :" + "Index_deal = " + index_deal);
		ActorItem item = ActorItem.recvActorItemFormServer(dis);
		m_items_other[index_deal] = item;
	}

	/**
	 * A将物品放置到交易栏
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvLaydownGoodsSTA(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvLaydownGoodsSTA");
		short state = dis.readShort();
		String info = dis.readUTF();
		Tools.debugPrintln("recvLaydownGoodsSTA :" + "State = " + state + " Info = " + info);
		if (state == 0) {
			if (!info.equals("")) {
				GameManager.getInstance().addMessage(info);
			}
			return;
		}
		int index_deal = dis.readInt();
		int index_bag = dis.readInt();
		Tools.debugPrintln("recvLaydownGoodsSTA :" + "DealIndex = " + index_deal + " BagIndex = " + index_bag);
		m_items_bag_temp[index_bag] = m_items_self[index_deal];
		m_items_self[index_deal] = null;
	}

	/**
	 * A 放下道具，Server对B的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvLaydownGoodsSTB(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvLaydownGoodsSTB");
		int index_deal = dis.readInt();
		Tools.debugPrintln("recvLaydownGoodsSTB index_deal = " + index_deal);
		m_items_other[index_deal] = null;
	}

	/**
	 * A放置金钱，Server对A的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvLayMoneySTA(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvLayMoneySTA ");
		short state = dis.readShort();
		String infos = dis.readUTF();
		Tools.debugPrintln("recvLayMoneySTA state = " + state + " Infos = " + infos);
		if (state == 0) {
			if (!infos.equals("")) {
				GameManager.getInstance().addMessage(infos);
			}
		}
		else {
			// 将当前放置的金钱放置到金钱栏里
		}
	}

	/**
	 * * A放置金钱，Server对B的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvLayMoneySTB(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvLayMoneySTB");
		m_money_game_other = dis.readLong();
		Tools.debugPrintln("recvLayMoneySTB money = " + m_money_game_other);
	}

	/**
	 * A发送完成交易请求，server对A的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvConfigSTA(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvConfigSTA");
		short state = dis.readShort();
		String infos = dis.readUTF();
		Tools.debugPrintln("recvConfigSTA State = " + state + " Info = " + infos);
		if (state == 0) {
			if (!infos.equals("")) {
				GameManager.getInstance().addMessage(infos);
			}
			return;
		}
		byte isFinshedDeal = dis.readByte();
		if (isFinshedDeal == 0) {
			Tools.debugPrintln("recvConfigSTA isFinshedDeal = " + isFinshedDeal);
		}
		else {
			actionConfig(dis);
		}
	}

	/**
	 * A发送完成交易请求，server对B的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvConfigSTB(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvConfigSTB ");
		byte isFinshedDeal = dis.readByte();
		Tools.debugPrintln("recvConfigSTB isFinshedDeal = " + isFinshedDeal);
		if (isFinshedDeal == 0) {
			final UIMessageBox messageBox = (UIMessageBox) UIManager.getInstance().createWindow(
							UIWindowFactory.WINDOW_MESSAGEBOX);
			messageBox.setMessage(m_name_other + "请求完成交易");
			messageBox.setCentre();
			messageBox.addListener(new UIListener() {
				public void actionPerformed()
				{
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
						// 同意完成交易
						GameManager.getInstance().m_game_message.sendDealConfig();
						UIManager.getInstance().removeWindow(messageBox);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(messageBox);
					}
				}
			});
		}
		else {// 双方已经完成交易，修改数据
			actionConfig(dis);
		}
	}

	/**
	 * 确定交易，将交易栏中的数据放置到自己的背包中
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void actionConfig(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("actionConfig ");
		int num = dis.readInt();
		Tools.debugPrintln("num = " + num);
		ActorItem[] bagItems = GameManager.getInstance().m_role.m_bag_goods;
		for (int i = 0; i < m_items_bag_temp.length; i++) {
			bagItems[i] = m_items_bag_temp[i];
		}
		m_bag_handler.resetBagData();
		for (int i = 0; i < num; i++) {
			ActorItem item = ActorItem.recvActorItemFormServer(dis);
			GameManager.getInstance().m_role.addActorItemToRole(item);
		}
		//
//		GameManager.getInstance().m_role.m_money_game -= dis.readLong();
		//
		UIManager.getInstance().removeWindow(m_messageBox_leftsoft);
		UIManager.getInstance().removeWindow(m_messageBox_rightsoft);
		close();
		GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "交易完成");
	}

	/**
	 * A取消交易，Server对A的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvCancelSTA(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvCancelSTA");
		short state = dis.readShort();
		String info = dis.readUTF();
		Tools.debugPrintln("recvCancelSTA State = " + state + " Info = " + info);
		if (state == 0) {
			if (!info.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
			}
			return;
		}
		GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "您取消了交易");
		actionCancel();
	}

	/**
	 * A取消交易，Server对B的响应
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvCancelSTB(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvCancelSTB");
		String info = dis.readUTF();
		if (!info.equals("")) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
		}
		else {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, m_name_other + "取消了交易");
		}
		actionCancel();
	}

	// 将交易栏数据取回，并退出
	private void actionCancel()
	{
		m_items_other = null;
		UIManager.getInstance().removeWindow(m_messageBox_leftsoft);
		UIManager.getInstance().removeWindow(m_messageBox_rightsoft);
		close();
	}

}
